package com.zzx.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.zzx.common.domain.CommonResult;
import com.zzx.pojo.UserOrder;
import com.zzx.service.IOrderService;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @version 1.0
 * @Author ZZX
 * @Date 2023/10/3 18:17
 */
@Tag(name = "订单接口", description = "订单模块")
@RestController
@RequestMapping("/order")
public class OrderController
{
    @Resource
    IOrderService orderService;

    /**
     * 获取某个人的订单列表
     *
     * @param userId
     * @param orderState
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("获取某个人的订单列表")
    @HystrixCommand(fallbackMethod = "hystrixGetOnesOrderList")
    @GetMapping("/getOnesOrderList")
    public CommonResult getOnesOrderList(@RequestParam("userId") Integer userId, @RequestParam("orderState") String orderState)
    {
        return orderService.getOnesOrderList(userId, orderState);
    }

    public CommonResult hystrixGetOnesOrderList(@RequestParam("userId") Integer userId, @RequestParam("orderState") String orderState)
    {
        return CommonResult.error().message("没有找到订单");
    }

    /**
     * 根据订单id查询订单详情
     *
     * @param orderId
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("根据订单id查询订单详情")
    @HystrixCommand(fallbackMethod = "hystrixGetOrderById")
    @GetMapping("/getOrderById/{orderId}")
    public CommonResult getOrderById(@PathVariable("orderId") Integer orderId)
    {
        return orderService.getOrderById(orderId);
    }

    public CommonResult hystrixGetOrderById(@PathVariable("orderId") Integer orderId)
    {
        return CommonResult.error().message("没有找到订单");
    }

    /**
     * 检测是否存在未处理的相同订单
     *
     * @param userId
     * @param goodsId
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("检测是否存在未处理的相同订单")
    @HystrixCommand(fallbackMethod = "hystrixGetOnesExistOrder")
    @GetMapping("/getOnesExistOrder")
    public CommonResult getOnesExistOrder(@RequestParam("userId") Integer userId, @RequestParam("goodsId") Integer goodsId)
    {
        return orderService.getOnesExistOrder(userId, goodsId);
    }

    public CommonResult hystrixGetOnesExistOrder(@RequestParam("userId") Integer userId, @RequestParam("goodsId") Integer goodsId)
    {
        return CommonResult.error().message("不存在相同的订单, 请重试");
    }

    /**
     * 新增订单
     *
     * @param userOrder
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("新增订单")
    @HystrixCommand(fallbackMethod = "hystrixMakeOrder")
    @PostMapping("/makeOrder")
    public CommonResult makeOrder(@RequestBody UserOrder userOrder)
    {
        return orderService.makeOrder(userOrder);
    }

    public CommonResult hystrixMakeOrder(@RequestBody UserOrder userOrder)
    {
        return CommonResult.error().message("商品已经超出库存, 不能创建订单, 请联系客服");
    }

    /**
     * 取消订单
     *
     * @param userOrder
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("取消订单")
    @HystrixCommand(fallbackMethod = "hystrixCancelOrder")
    @PutMapping("/cancelOrder")
    public CommonResult cancelOrder(@RequestBody UserOrder userOrder)
    {
        return orderService.cancelOrder(userOrder);
    }

    public CommonResult hystrixCancelOrder(@RequestBody UserOrder userOrder)
    {
        return CommonResult.error().message("商品已经超出库存, 不能取消订单, 请联系客服");
    }

    /**
     * 购买订单
     *
     * @param userOrder
     * @return
     */
    @Tag(name = "订单接口")
    @ApiOperation("购买订单")
    @HystrixCommand(fallbackMethod = "hystrixPayOrder")
    @PutMapping("/payOrder")
    public CommonResult payOrder(@RequestBody UserOrder userOrder)
    {
        return orderService.payOrder(userOrder);
    }

    public CommonResult hystrixPayOrder(@RequestBody UserOrder userOrder)
    {
        return CommonResult.error().message("因为商品已经超出库存, 不能支付，请联系客服");
    }
}